package demo1;

public class MySingleList {
    static class ListNode{
    public int val ;
    public ListNode next ;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head ;

//    public void createList(){
//        ListNode node1 = new ListNode(12) ;
//        ListNode node2 = new ListNode(52) ;
//        ListNode node3 = new ListNode(10) ;
//        ListNode node4 = new ListNode(60) ;
//        node1.next = node2 ;
//        node2.next = node3 ;
//        node3.next = node4 ;
//        this.head = node1 ;
//
//    }
  public void display(){
        ListNode ret = this.head ;
        while(ret != null){
            System.out.print(ret.val + "  ");
            ret = ret.next ;
        }
      System.out.println();

  }
  public int size(){
        int count = 0;
        ListNode ret = this.head;
        while(ret != null ){
            count++ ;
            ret = ret.next ;
        }
        return count ;
  }
  public boolean contains(int val){
      ListNode ret = this.head;
      while(ret != null ){
          if(ret.val == val){
              return true ;
          }
          ret = ret.next ;
      }
    return false ;
  }
  public void addFirst(int data){
        ListNode node = new ListNode(data) ;
       node.next = this.head ;
        this.head = node ;
  }
   public void addLast(int data){
      if(this.head == null){
          this.head = new ListNode(data);
          return;
      }
       ListNode ret = this.head ;
       while (ret.next != null){
           ret = ret.next ;
       }
       ret.next = new ListNode(data) ;
   }
   public void addIndex(int index ,int data){
      if(index < 0 || index > size()){
          System.out.println("index不合法");
          return;
      }
      if(index == 0){
          addFirst(data);
          return;
      }
      if(index == size()){
          addLast(data);
          return;
      }
      int count = 0;
      ListNode node = new ListNode(data) ;
      ListNode ret = this.head ;
      while(count != index - 1){
          ret = ret.next ;
          count ++ ;
      }
      node.next = ret.next ;
      ret.next = node ;
   }
   public void remove(int key){
      if(this.head == null){
          System.out.println("链表为空");
          return;
      }
     if(this.head.val == key){
         this.head = this.head.next ;
          return;
     }
     ListNode ret = searchPrev(key) ;
     if(ret == null){
         System.out.println("没有你要删除的数据");
         return;
     }
     ret.next = ret.next.next ;
   }
   public ListNode searchPrev(int key){
      ListNode ret = this.head ;
      while (ret.next != null){
          if(ret.next.val == key){
              return ret ;
          }
       ret = ret.next ;
      }
      return null ;
   }
   public void removeAllKey(int key){
      if(this.head == null){
          System.out.println("链表为空");
          return;
      }
      ListNode ret = this.head ;
      while (ret.next != null ){
          if(ret.next.val == key){
              ret.next = ret.next.next ;
          }else{
              ret = ret.next ;
          }
      }
      if(this.head.val == key){
          this.head = this.head.next ;
      }
   }
   public void clear(){
      this.head = null ;
   }

}
